package com.itunion.ddd.infra.gateway.impl;

import com.itunion.ddd.domain.item.gateway.TbItemCategoryGateway;
import com.itunion.ddd.domain.item.TbItemCategory;
import com.itunion.ddd.infra.convertor.TbItemCategoryInfraConverter;
import com.itunion.ddd.infra.gateway.database.dataobject.TbItemCategoryDO;
import com.itunion.ddd.infra.gateway.database.mapper.TbItemCategoryMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 商品分类信息
 *
 * @author liangliang.zheng
 * @date 2020-07-02 12:20 PM
 */
@Component
@Slf4j
public class TbItemCategoryGatewayImpl implements TbItemCategoryGateway {
    @Resource
    private TbItemCategoryMapper tbItemCategoryMapper;

    @Override
    public TbItemCategory selectById(Long id) {
        TbItemCategoryDO tbItemCategoryDO = tbItemCategoryMapper.selectById(id);
        if (!Objects.isNull(tbItemCategoryDO)) {
            return TbItemCategoryInfraConverter.toTbItemCategory(tbItemCategoryDO);
        }
        return null;
    }

    @Override
    public TbItemCategory save(TbItemCategory tbItemCategory) {
        TbItemCategoryDO tbItemCategoryDO = TbItemCategoryInfraConverter.toTbItemCategoryDO(tbItemCategory);
        // TODO 插入一条数据
        tbItemCategoryMapper.insert(tbItemCategoryDO);
        // TODO 根据ID查询数据详情
        return this.selectById(tbItemCategoryDO.getId());
    }
}
